package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.mapper.BaseAttrValueMapper;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/***
 * 平台属性值的Service层实现类
 */
@Service
public class BaseAttrValueServiceImpl implements BaseAttrValueService {

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    /**
     * 条件分页查询
     *
     * @param baseAttrValue
     * @param pageNum
     * @param size
     * @return
     */
    @Override
    public IPage<BaseAttrValue> search(BaseAttrValue baseAttrValue, Integer pageNum, Integer size) {
        //条件构造器
        LambdaQueryWrapper<BaseAttrValue> queryWrapper = buildQueryParam(baseAttrValue);

        //分页对象
        Page<BaseAttrValue> page = new Page<>(pageNum, size);

        //条件分页查询操作
        IPage<BaseAttrValue> baseAttrValueIPage = baseAttrValueMapper.selectPage(page, queryWrapper);

        //返回
        return baseAttrValueIPage;
    }

    /**
     * 条件查询
     *
     * @param baseAttrValue
     * @return
     */
    @Override
    public List<BaseAttrValue> search(BaseAttrValue baseAttrValue) {
        //条件构造器
        LambdaQueryWrapper<BaseAttrValue> queryWrapper = buildQueryParam(baseAttrValue);

        //条件查询操作
        List<BaseAttrValue> baseAttrValues = baseAttrValueMapper.selectList(queryWrapper);

        //返回结果
        return baseAttrValues;
    }

    /**
     * 构建条件参数
     * @param baseAttrValue
     * @return
     */
    private LambdaQueryWrapper<BaseAttrValue> buildQueryParam(BaseAttrValue baseAttrValue) {
        //条件构造器
        LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();

        //判断id
        if(null != baseAttrValue.getId()){
            queryWrapper.eq(BaseAttrValue::getId,baseAttrValue.getId());
        }

        //判断名字
        if(null != baseAttrValue.getValueName()){
            queryWrapper.like(BaseAttrValue::getValueName,baseAttrValue.getValueName());
        }

        //判断属性id
        if (null != baseAttrValue.getAttrId()) {
            queryWrapper.eq(BaseAttrValue::getAttrId,baseAttrValue.getAttrId());
        }

        //返回
        return queryWrapper;
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param size
     * @return
     */
    @Override
    public IPage<BaseAttrValue> page(Integer pageNum, Integer size) {
        //创建分页对象
        Page<BaseAttrValue> page = new Page<>(pageNum, size);

        //分页查询
        IPage<BaseAttrValue> baseAttrValueIPage = baseAttrValueMapper.selectPage(page,null);

        //返回数据
        return baseAttrValueIPage;
    }

    /**
     * 根据主键id删除
     *
     * @param id
     */
    @Override
    public void removeById(Long id) {
        //校验参数
        if (id == null) {
            throw new GmallException("参数错误", null);
        }

        //删除操作
        int delete = baseAttrValueMapper.deleteById(id);

        //是否成功
        if (delete < 0) {
            throw new GmallException("删除失败", null);
        }
    }

    /**
     * 根据主键id修改
     *
     * @param baseAttrValue
     */
    @Override
    public void edit(BaseAttrValue baseAttrValue) {
        //校验参数
        if(null == baseAttrValue){
            throw new GmallException("参数错误", null);
        }

        //修改操作
        int update = baseAttrValueMapper.updateById(baseAttrValue);

        //是否成功
        if (update < 0) {
            throw new GmallException("修改失败", null);
        }

    }

    /**
     * 新增数据
     *
     * @param baseAttrValue
     */
    @Override
    public void save(BaseAttrValue baseAttrValue) {
        //校验参数
        if (baseAttrValue == null ||
                StringUtils.isEmpty(baseAttrValue.getValueName())) {
            throw new GmallException("参数错误", null);
        }

        //新增操作
        int insert = baseAttrValueMapper.insert(baseAttrValue);

        //是否成功
        if(insert<=0){
            throw new GmallException("新增失败", null);
        }
    }

    /**
     * 根据主键id查询
     *
     * @param id
     * @return
     */
    @Override
    public BaseAttrValue getById(Long id) {
        return baseAttrValueMapper.selectById(id);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @Override
    public List<BaseAttrValue> list() {
        return baseAttrValueMapper.selectList(null);
    }
}
